package com.ruoyi.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProcessInstance;
import com.ruoyi.service.ProcessInstanceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程实例Controller
 * 
 * @author ruoyi
 * @date 2021-04-30
 */
@Controller
@RequestMapping("/process/instance")
public class ProcessInstanceController extends BaseController {
    private String prefix = "instance";

    @Autowired
    private ProcessInstanceService processInstanceService;
//    @Autowired
//    private IActFlowRecordService actFlowRecordService;

    @RequiresPermissions("process:instance:view")
    @GetMapping()
    public String instance() {
        return prefix + "/instance";
    }

    /**
     * 查询流程实例列表
     */
    @RequiresPermissions("process:instance:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessInstance processInstance) {
        startPage();
        List<ProcessInstance> list = processInstanceService.selectActProcinstList(processInstance);
        return getDataTable(list);
    }

    /**
     * 导出流程实例列表
     */
    @RequiresPermissions("process:instance:export")
    @Log(title = "流程实例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessInstance processInstance)
    {
        List<ProcessInstance> list = processInstanceService.selectActProcinstList(processInstance);
        ExcelUtil<ProcessInstance> util = new ExcelUtil<ProcessInstance>(ProcessInstance.class);
        return util.exportExcel(list, "流程实例数据");
    }

    /**
     * 新增流程实例
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存流程实例
     */
    @RequiresPermissions("process:instance:add")
    @Log(title = "流程实例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessInstance processInstance) {
        return toAjax(processInstanceService.insertActProcinst(processInstance));
    }

    /**
     * 修改流程实例
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        ProcessInstance processInstance = processInstanceService.selectActProcinstById(id);
        mmap.put("processInstance", processInstance);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程实例
     */
    @RequiresPermissions("process:instance:edit")
    @Log(title = "流程实例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessInstance processInstance) {
        return toAjax(processInstanceService.updateActProcinst(processInstance));
    }

    /**
     * 删除流程实例
     */
    @RequiresPermissions("process:instance:remove")
    @Log(title = "流程实例", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) throws Exception {
//        return toAjax(processInstanceService.deleteActProcinstById(ids));
        return null;
    }

    @RequiresPermissions("process:instance:view")
    @GetMapping("/HI")
    public String HIPROCINST() {
        return prefix + "/HANDLEHIPROCINST";
    }
    /**
     * 查询【办理历史】列表
     */
    @RequiresPermissions("process:instance:handleHiList")
    @PostMapping("/handleHiList")
    @ResponseBody
    public TableDataInfo handleHiList(ProcessInstance processInstance) {
        startPage();
        List<ProcessInstance> list = processInstanceService.selectHandleHiProcinstList(processInstance);
        return getDataTable(list);
    }


    /**
     * 任务处理图片信息
     */

    @Log(title = "任务处理图片信息", businessType = BusinessType.OTHER)
    @GetMapping( "/getHandleTaskImgInfo/{processInstanceId}")
    public String  getHandleTaskImgInfo(@PathVariable("processInstanceId")String processInstanceId, ModelMap mmap) {
        //Map<String, Object> actHandleProcessImgInfo = actRuTaskService.getActHandleProcessImgInfo(taskIds);
        Map<String, Object> actHandleProcessImgInfo = new HashMap<String, Object>();
        actHandleProcessImgInfo.put("processInstanceId",processInstanceId);
        mmap.put("handleTaskImgInfo", actHandleProcessImgInfo);
        return  "activity/TASK/processimg";
    }

    /**
     * 删除【流程撤回】
     */
    @RequiresPermissions("process:instance:remove")
    @Log(title = "流程实例", businessType = BusinessType.DELETE)
    @PostMapping( "/withDraw")
    @ResponseBody
    public AjaxResult withDraw(String businessId, String tableName) throws Exception {
//        processInstanceService.deleteActProcinstById(businessId,tableName);
        return toAjax(1);
    }

}
